﻿using Common.Dtos;
using Prism.Commands;
using Prism.Events;
using Prism.Ioc;
using Prism.Mvvm;
using Sigma.Admin.Client.Events;
using Sigma.Admin.Client.Extensions;
using Sigma.Admin.Client.Services;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sigma.Admin.Client.ViewModels
{
    public class MenuRegionViewModel : NavigationViewModel
    {
        private readonly IMenuService _menuService;
        private ObservableCollection<string> _messages;
        public ObservableCollection<string> Messages
        {
            get { return _messages; }
            set { SetProperty(ref _messages, value); }
        }

        private List<MenuDto> menus;

        public List<MenuDto> Menus
        {
            get { return menus; }
            set { menus = value; RaisePropertyChanged(); }
        }
        public MenuRegionViewModel(IEventAggregator ea, IContainerProvider containerProvider, IMenuService service) :base(ea, containerProvider)
        {
            //Menus = DataSeed.BuildMenus();
            _ea.GetEvent<SearchSentEvent>().Subscribe(MessageReceived);
            _menuService= service;
            _ea.ResgiterMessage(InitDataAsync, "UpdateMenu");
        }
        //目前做二级菜单的搜索
        private void MessageReceived(SearchModel searchModel)
        {
            if (!string.IsNullOrEmpty(searchModel.Message)&&searchModel.DrowDownsAct!=null)
            {
                var message = searchModel.Message;
                var search = new List<DrowDown>();
                foreach(var item in Menus)
                {
                    var res = item.children.FindAll(x => x.TitleName!.Contains(message));
                    if (res.Any())
                    {
                        item.IsExpander = true;
                        foreach(var item2 in res)
                        {
                            search.Add(new DrowDown(item2.TitleName, item2.Target));
                        }
                    }
                }
                searchModel.DrowDownsAct(search);
            }
        }

        private async void InitDataAsync(MessageModel mes=null)
        {
            var res = await _menuService.GetMenu();
            if (res.Status)
            {
                Menus = BuildMenus(res.Result);
            }
            else
            {
                _ea.SendMessage(res.Message);
            }
        }

        //选择要构建的启始level
        private List<MenuDto> BuildMenus(List<MenuDto> list,int level = 0)
        {
            var result = new List<MenuDto>();
            for (; level < 2; level++)
            {
                var thisLevel = list.Where(x => x.Level == level);
                if (level == 0)
                {
                    result.AddRange(thisLevel);
                }
                else
                {
                    foreach (var i in result.Where(x => x.Level == level - 1))
                    {
                        var res = thisLevel.Where(x => x.PId == i.Id);
                        var child = new List<MenuBaseDto>();
                        child.AddRange(res);
                        i.Children = child;
                    }
                }
            }
            return result;
        }
    }
}
